function maxL2err(u,n,term)

if(exist('data.mat','file'))
    load data;
else
    disp('Data file reading error!')
end

switch term
    case 4
        u0p=u(1:n-1); u1p=u(n:2*n-2);   u2p=u(2*n-1:3*n-3);   u3p=u(3*n-2:4*n-4);
        err = zeros(n-1,1);
        h=1/n;
        for ii=1:n-1
            x=ii*h;
            errorhd1=@(z) (log(1+z*x)./log(1+z) ...
                 - u0p(ii) - u1p(ii)*z/sig - u2p(ii)/sqrt(2)*(z.^2/sig^2-1) ...
                 - u3p(ii)/sqrt(6)*(z.^3/sig^3-3*z/sig)).^2;
%            errorhd2=@(z) ((1+z)*x/(1+z*x) ...
%                 - u0p(ii) - u1p(ii)*z/sig - u2p(ii)/sqrt(2)*(z.^2/sig^2-1) ...
%                 - u3p(ii)/sqrt(6)*(z.^3/sig^3-3*z/sig)).^2;
            tmphd1=@(z) errorhd1(z).*gaussian(z);
%            tmphd2=@(z) errorhd2(z).*gaussian(z);
            epsilon=10^-4;
            err(ii)=quadl(tmphd1,-1,-epsilon,10^-10);%+quadl(tmphd2,-epsilon,epsilon,10^-10)+quadl(tmphd1,epsilon,1,10^-10);
        end
        err=sqrt(max(err));
        disp(err)

    case 5
        u0p=u(1:n-1); u1p=u(n:2*n-2);   u2p=u(2*n-1:3*n-3);   u3p=u(3*n-2:4*n-4); u4p=u(4*n-3:5*n-5);
        err = zeros(n-1,1);
        h=1/n;
        for ii=1:n-1
            x=ii*h;
            errorhd1=@(z) (log(1+z*x)./log(1+z) ...
                 - u0p(ii) - u1p(ii)*z/sig - u2p(ii)/sqrt(2)*(z.^2/sig^2-1) ...
                 - u3p(ii)/sqrt(6)*(z.^3/sig^3-3*z/sig)...
                 - u4p(ii)/sqrt(24)*(z.^4/sig^4-6*z.^2/sig^2+3)).^2;
%            errorhd2=@(z) ((1+z)*x/(1+z*x) ...
%                 - u0p(ii) - u1p(ii)*z/sig - u2p(ii)/sqrt(2)*(z.^2/sig^2-1) ...
%                 - u3p(ii)/sqrt(6)*(z.^3/sig^3-3*z/sig)).^2;
            tmphd1=@(z) errorhd1(z).*gaussian(z);
%            tmphd2=@(z) errorhd2(z).*gaussian(z);
            epsilon=10^-4;
            err(ii)=quadl(tmphd1,-1,-epsilon,10^-10);%+quadl(tmphd2,-epsilon,epsilon,10^-10)+quadl(tmphd1,epsilon,1,10^-10);
        end
        err=sqrt(max(err));
        disp(err)
end


% unknownsposition=(1:n-1)/n;
% uex =[uex{1}(unknownsposition');uex{2}(unknownsposition');uex{3}(unknownsposition');uex{4}(unknownsposition')];
% 
% disp('max error')
% err=max(abs(uex(1:term*(n-1))-u));
% disp(err)
% 
% disp('L2 error')
% err=sqrt(sum((uex(1:term*(n-1))-u).^2/n));
% disp(err)




